<template>
    <div class="app-container">
        <!--查询表单-->
        <div class="search-div" style="margin-bottom: 10px;">
            <el-form label-width="70px" size="small">
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="角色名称">
                            <el-input style="width: 100%" v-model="searchObj.roleName" placeholder="角色名称"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row style="display:flex">
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="fetchData(page)">搜索</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetData">重置</el-button>
                </el-row>
            </el-form>
        </div>

        <!-- 工具条 -->
        <div class="tools-div">
            <el-button type="success" icon="el-icon-plus" size="mini" @click="add"
                :disabled="$hasBP('bnt.role.add') === false">添 加</el-button>
        </div>
        <!-- 添加/修改对话框 -->
        <el-dialog title="添加/修改" :visible.sync="dialogVisible" width="40%">
            <el-form ref="sysRole" :model="sysRole" label-width="150px" size="small" style="padding-right: 40px">
                <el-form-item label="角色名称" prop="roleName" :rules="{
                        required: true,
                        message: '角色名称不能为空',
                        trigger: 'blur',
                    }">
                    <el-input v-model="sysRole.roleName" @blur="CheckName(sysRole.roleName)" />
                </el-form-item>
                <el-form-item label="角色编码" prop="roleCode" :rules="{
                        required: true,
                        message: '角色编码不能为空',
                        trigger: 'blur',
                    }">
                    <el-input v-model="sysRole.roleCode" />
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="CloseDialog" size="small" icon="el-icon-refresh-right">取 消</el-button>
                <el-button type="primary" icon="el-icon-check" @click="saveOrUpdate()" size="small">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 列表数据 -->
        <el-table v-loading="listLoading" :data="list" stripe border style="width: 100%;margin-top: 10px;">

            <el-table-column label="序号" width="70" align="center">
                <template slot-scope="scope">
                    {{ (page - 1) * limit + scope.$index + 1 }}
                </template>
            </el-table-column>

            <el-table-column prop="roleName" label="角色名称" />
            <el-table-column prop="roleCode" label="角色编码" />
            <el-table-column prop="createTime" label="创建时间" width="160" />
            <el-table-column label="操作" width="200" align="center">
                <template slot-scope="scope">
                    <el-button type="primary" icon="el-icon-edit" :disabled="$hasBP('bnt.role.update') === false"
                        size="mini" @click="edit(scope.row.id)" title="修改" />
                    <el-button type="danger" icon="el-icon-delete" :disabled="$hasBP('bnt.role.remove') === false"
                        size="mini" @click="removeDataById(scope.row.id)" title="删除" />
                    <el-button type="warning" icon="el-icon-baseball" size="mini" @click="showAssignAuth(scope.row)"
                        title="分配权限" :disabled="$hasBP('bnt.role.assignAuth') === false" />
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-pagination :current-page="page" :total="total" :page-size="limit"
            style="padding: 30px 0; text-align: center;" layout="total, prev, pager, next, jumper"
            @current-change="fetchData" />
    </div>
</template>

<script>
import api from '@/api/role/role.js'
const defaultForm = {
    id: "",
    roleName: "",
    roleCode: 0,
};
export default {
    data() {
        return {
            list: [],
            total: 0,
            page: 1,
            limit: 5,
            searchObj: {},
            sysRole: {},
            isAddOrUpdate: 1,
            dialogVisible: false
        }
    },
    created() {
        this.fetchData(1);
    },
    methods: {
        fetchData(page) {
            this.page = page;
            api.getPageList(this.page, this.limit, this.searchObj).then(response => {
                // console.log(response);
                this.list = response.data.records;
                this.total = response.data.total;
                this.page = response.data.current;
                this.limit = response.data.size;
            })
        },
        showAssignAuth(row) {
            this.$router.push('/system/assignAuth?id=' + row.id + '&roleName=' + row.roleName);
        },

        // 删除操作
        removeDataById(id) {
            this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                api.removeId(id)
                    .then((response) => {
                        // 提示
                        this.$message({
                            type: "success",
                            message: "删除成功!",
                        });
                        // 刷新页面
                        this.fetchData(this.page);
                    });
            });
        },
        add() {
            this.isAddOrUpdate = 1;
            this.dialogVisible = true;

        },
        edit(id) {
            this.isAddOrUpdate = 2;
            this.dialogVisible = true;
            api.findRoleById(id)
                .then((response) => {
                    // console.log(response.data)
                    this.sysRole = response.data;
                });
        },
        CheckName(name) {
            if (name === "") {
                return;
            }

            if (this.sysRole.id === "") {
                api.selectByName(name).then((response) => {

                    if (response.data !== "") {
                        this.$message.error(response.data);
                    }
                });
            }
        },
        saveOrUpdate() {
            if (this.isAddOrUpdate === 1) {
                this.addRole()
            } else {
                this.updateRole()
            }


        },
        addRole() {
            this.$refs.sysRole.validate((valid) => {
                if (valid) {
                    api.addRole(this.sysRole).then((response) => {
                        // 提示
                        this.$message({
                            type: "success",
                            message: "保存成功!",
                        });
                        this.sysRole = {};
                        this.dialogVisible = false;
                        // 刷新页面
                        this.fetchData(this.page);
                    });
                }
            });
        },
        updateRole() {
            this.$refs.sysRole.validate((valid) => {
                if (valid) {
                    api.updateRole(this.sysRole).then((response) => {
                        // 提示
                        this.$message({
                            type: "success",
                            message: "保存成功!",
                        });
                        this.sysRole = {};
                        this.dialogVisible = false;
                        // 刷新页面
                        this.fetchData(this.page);
                    });
                }
            });
        },

        CloseDialog() {
            this.sysRole = {};
            this.dialogVisible = false
        },
        resetData() {
            this.searchObj.roleName = ''
        }

    }
}
</script>

<style></style>